package com.example.sls.dao.user;

import com.example.sls.pojo.user.ExerciseData;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 运动数据表(ExerciseData)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:54:55
 */
public interface ExerciseDataDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from exercise_data where id=#{id}")
  ExerciseData queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param exerciseData 查询条件
   * @param pageSize     分页对象
   * @param offset       偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from exercise_data where 1=1" +
          " and (id=#{exerciseData.id} or (isnull(#{exerciseData.id}) or #{exerciseData.id}=0))" +
          " and (user_id=#{exerciseData.userId} or (isnull(#{exerciseData.userId}) or #{exerciseData.userId}=0))" +
          " and (exercise_type=#{exerciseData.exerciseType} or (isnull(#{exerciseData.exerciseType}) or #{exerciseData.exerciseType}=''))" +
          " and (duration=#{exerciseData.duration} or (isnull(#{exerciseData.duration}) or #{exerciseData.duration}=0))" +
          " and (frequency=#{exerciseData.frequency} or (isnull(#{exerciseData.frequency}) or #{exerciseData.frequency}=0))" +
          " and (created_at=#{exerciseData.createdAt} or (isnull(#{exerciseData.createdAt}) or #{exerciseData.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<ExerciseData> queryAllByLimit(ExerciseData exerciseData, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from exercise_data")
  long count();

  /**
   * 新增数据
   *
   * @param exerciseData 实例对象
   * @return 影响行数
   */
  @Insert("insert into exercise_data (id,user_id,exercise_type,duration,frequency,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{userId}!=0 and !isnull(#{userId}),#{userId},null)," +
          "if(#{exerciseType}!='' and !isnull(#{exerciseType}),#{exerciseType},null)," +
          "if(#{duration}!=0 and !isnull(#{duration}),#{duration},null)," +
          "if(#{frequency}!=0 and !isnull(#{frequency}),#{frequency},null)," +
          "if(#{createdAt}!='' and !isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(ExerciseData exerciseData);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param exerciseData 实例对象
   * @return 影响行数
   */
  @Update("update exercise_data set " +
          "user_id=if(isnull(#{userId}) or #{userId}=0,user_id,#{userId})," +
          "exercise_type=if(isnull(#{exerciseType}) or #{exerciseType}='',exercise_type,#{exerciseType})," +
          "duration=if(isnull(#{duration}) or #{duration}=0,duration,#{duration})," +
          "frequency=if(isnull(#{frequency}) or #{frequency}=0,frequency,#{frequency})," +
          "created_at=if(isnull(#{createdAt}) or #{createdAt}='',created_at,#{createdAt})" +
          " where id=#{id}")
  int update(ExerciseData exerciseData);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from exercise_data where id=#{id}")
  int deleteById(Integer id);
}

